mini.parse();

function GoodsSupplierFormHandler() {
};

GoodsSupplierFormHandler.instance = new GoodsSupplierFormHandler();

//获得全局的供货商表单对象
GoodsSupplierFormHandler.goodsSupplierForm = new mini.Form("goodsSupplierForm");
GoodsSupplierFormHandler.formData = {};
//全局id
GoodsSupplierFormHandler.__bsiId = null;

// 供货商状态数据
GoodsSupplierFormHandler.bsiStateData = [
	{text: "有效",value: 1},
	{text: "无效",value: 0}
];
mini.get("bsiState").setData(GoodsSupplierFormHandler.bsiStateData);

//创建上传供货商图片控件
SWFUploadTools.createSWFUBtn(__realpath+__supplierImgPath, null, "bsiUpload", uploadBsiLogoSuccess);

/**
 * 保存（添加或编辑）数据的方法
 */
GoodsSupplierFormHandler.save = function(){
	var goodsSupplierData = GoodsSupplierFormHandler.goodsSupplierForm.getData();
	goodsSupplierData.bsiId = GoodsSupplierFormHandler.formData.bsiId;
	
    GoodsSupplierFormHandler.goodsSupplierForm.validate();
    if (GoodsSupplierFormHandler.goodsSupplierForm.isValid() == false) return;
    
    var json = mini.encode(goodsSupplierData);
    $.ajax({
        url: __root + "/saveOrUpdateGoodsSupplier.do",
        type: "POST",
        data: {
        	goodsSupplierInfo: json,
        	lslUser: gbOperateUser,
    		lslAddress: gbOperateIp
        },
        cache: false,
        success: function (text) {
        	MiniuiTools.miniInfo("系统提示","保存成功！", function(action){
        		GoodsSupplierFormHandler.closeCurrentWin("save");
        	});
        },
        error: function (jqXHR, textStatus, errorThrown) {
        	MiniuiTools.miniError("系统提示", jqXHR.responseText, function(){
            	GoodsSupplierFormHandler.closeCurrentWin();
            });
        }
    });
    
}

/**
 * 标准方法接口定义
 * @param {} data
 */
GoodsSupplierFormHandler.passData = function(data) {
    if (data.action == "edit") {
        //跨页面传递的数据对象，克隆后才可以安全使用
        data = mini.clone(data);
        GoodsSupplierFormHandler.formData.bsiId = data.bsiId;
        //根据id获取对应的供货商信息
        $.ajax({ 
            url: __root + "/getGoodsSupplierById.do?bsiId=" + data.bsiId,
            cache: false,
            dataType: 'json',
            success: function (text) {
            	//设置全局id
            	GoodsSupplierFormHandler.__bsiId = data.bsiId;
            	//设置form表单
                GoodsSupplierFormHandler.goodsSupplierForm.setData(text);
                if(text.bsiLogo != null && text.bsiLogo.length > 0){
                	$("#imgViewer").empty();
					
					var iframe = "<iframe src='" + __root + "/imgViewerContorller.do?basePath=" + __realpath+__supplierImgPath + "&newImgPath=" + text.bsiLogo + "' frameborder='0' border='0' style='margin:0;padding:0;width:100%;height:100%;'/>";
					$("#imgViewer").append(iframe);
					
					//启用删除图标按钮
					mini.get("delBsiLogoBtn").enable();
                }
            }
        });
    }else{
    	//默认供货商状态为有效
    	mini.get("bsiState").setValue(1);
    }
}

/**
 * 删除供货商图标
 */
GoodsSupplierFormHandler.delBsiLogo = function(){
	var bsiId = GoodsSupplierFormHandler.__bsiId;
	var bsiLogo = GoodsSupplierFormHandler.goodsSupplierForm.getData().bsiLogo;
	
	if(bsiId != null && bsiId.length > 0 && bsiLogo != null && bsiLogo.length > 0){
		$.ajax({
	        url: __root + "/delGoodsBsiLogo.do",
	        type: "POST",
	        data: {
	        	bsiId: bsiId,
	        	lslUser: gbOperateUser,
	    		lslAddress: gbOperateIp
	        },
	        cache: false,
	        success: function (text) {
	        	 MiniuiTools.miniInfo("系统提示","删除成功！");
	        	 //清空路径
               	 mini.get("upload_bsiLogo").setValue(null);
               	 //清空预览框
               	 $("#imgViewer").empty();
				 
	        },
	        error: function (jqXHR, textStatus, errorThrown) {
	            MiniuiTools.miniError("系统提示", jqXHR.responseText);
	        }
	    });
	}else{
		MiniuiTools.miniWarning("系统提示","没有可删除的图标资源！");
	}
}

/**
 * 关闭当前窗口
 * @param {} action
 */
GoodsSupplierFormHandler.closeCurrentWin = function(action) {
    if (window.CloseOwnerWindow) window.CloseOwnerWindow(action);
    else window.close();
    GoodsSupplierFormHandler.__bsiId = null;
}

/**
 * 关闭mini窗口
 */
GoodsSupplierFormHandler.closeMiniWin = function(){
	GoodsSupplierFormHandler.closeCurrentWin("cancel");
	GoodsSupplierFormHandler.__bsiId = null;
}

/**
 * 处理上传成功后的图片
 * @param {} file
 * @param {} serverData
 */
function uploadBsiLogoSuccess(file, serverData) {
	MiniuiTools.miniHideLoading();
	MiniuiTools.miniInfo("系统提示","文件上传成功！");
	
	$("#imgViewer").empty();
	var fdata = mini.decode(serverData);
	var newPath = fdata.imgPath;	
	var iframe = "<iframe src='" + __root + "/imgViewerContorller.do?basePath=" + __realpath+__supplierImgPath + "&newImgPath=" + newPath + "' frameborder='0' border='0' style='margin:0;padding:0;width:100%;height:100%;'/>";
	$("#imgViewer").append(iframe);
	
	mini.get("upload_bsiLogo").setValue(newPath);
}